package net.huashitong.gradePoint.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.gradePoint.entity.GradePoint;
import net.huashitong.gradePoint.filter.GradePointFilter;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;

import static net.huashitong.utils.UtilsTools.getCurrentDateTime2;

@Repository
public class GradePointDao extends StringDao<GradePoint> {

    public List<GradePoint> queryAllGradePoint(GradePointFilter filter) {
        GenericQuery query = createQuery("obj");
        //查询所有绩效考核
        return query.listResult(filter);
    }

    public List<GradePoint> queryGradePoint(GradePointFilter filter) {
        GenericQuery query = createQuery("obj");
        if (StringUtils.isNotBlank(filter.getCheckObject())){
            query.append(" and checkObject like:checkObject").setParam("checkObject",
                    StringUtils.getLikewords(filter.getCheckObject()));
        }
        if (StringUtils.isNotBlank(filter.getCheckType())){
            query.append(" and checkType like:checkType").setParam("checkType",
                    StringUtils.getLikewords(filter.getCheckType()));
        }
        if (StringUtils.isNotBlank(filter.getSoucre())){
            query.append(" and soucre =:soucre").setParam("soucre",
                    filter.getSoucre());
        }
        if (StringUtils.isNotBlank(filter.getTaskContent())){
            query.append(" and taskContent like:taskContent").setParam("taskContent",
                    StringUtils.getLikewords(filter.getTaskContent()));
        }
        if (StringUtils.isNotBlank(filter.getTaskId())){
            query.append(" and taskId =:taskId").setParam("taskId",
                    filter.getTaskId());
        }
        if (StringUtils.isNotBlank(filter.getTaskName())){
            query.append(" and taskName like:taskName").setParam("taskName",
                    StringUtils.getLikewords(filter.getTaskName()));
        }
        if (StringUtils.isNotBlank(filter.getCheckObjectId())){
            query.append(" and checkObjectId =:checkObjectId").setParam("checkObjectId",
                    filter.getCheckObjectId());
        }
        if(StringUtils.isNotBlank(filter.getStartDate())){
            if (StringUtils.isNotBlank(filter.getEndDate())) {
                query.append(" and unix_timestamp( createDate ) between unix_timestamp( '"+filter.getStartDate()+"') and unix_timestamp( '"+filter.getEndDate()+"' )");
            }else{
                query.append(" and unix_timestamp( createDate ) between unix_timestamp( '"+filter.getStartDate()+"') and unix_timestamp( '"+getCurrentDateTime2()+"' )");
            }
        }
        if (filter.getPaging() != null) {
            if (StringUtils.isBlank(filter.getPaging().getSortCol())) {
//                query.append(" ORDER BY  str_to_date( CONCAT(obj.updDate,' ',obj.updTime),'%Y-%m-%d %H:%i:%s') desc  ");
                filter.getPaging().setSortCol("createDate");
                filter.getPaging().setSortVal("desc");
            }
            if ("rank".equals(filter.getPaging().getSortCol())) {
                filter.getPaging().setSortCol("rank+0");
            }
            if ("soucre".equals(filter.getPaging().getSortCol())) {
                filter.getPaging().setSortCol("soucre+0");
            }
        }
        return query.listResult(filter);
    }

    /**
     * 根据Id  获取考核分
     *
     * @param id
     * @return
     */
    public List<GradePoint> getGradePointById(String id) {
        GenericQuery query = createQuery("obj");
        query.append("and obj.taskId=:taskId").setParam("taskId", id);
        return query.listResult();
    }

    /**
     * 根据多个id查询多个GradePoint
     *
     * @param ids
     * @return
     */
    public List<GradePoint> queryGradePointByIds(List<String> ids) {
        GenericQuery query = createQuery("obj");
        query.append("and obj.sysId in(:ids)").setParamList("ids", ids);
        return query.listResult();
    }

    /**
     * 根据考核类别查询
     *
     * @param checkType
     * @return
     */
    public List<GradePoint> queryGradePointByCheckType(String checkType) {
        GenericQuery query = createQuery("obj");
        query.append("and obj.checkType like:checkType").setParam("checkType",
                StringUtils.getLikewords(checkType));
        return query.listResult();
    }

    public List<GradePoint> queryChecked() {
        StringBuffer sql = new StringBuffer();
        sql.append("select " +
                "g.check_object checkObject," +
                "g.check_type checkType," +
                "g.soucre soucre," +
                "g.task_content taskContent," +
                "g.task_id taskId," +
                "g.task_name taskName," +
                "g.object_id objectId," +
                "g.rank rank," +
                "g.res_dept resDept," +
                "g.type_rank typeRank," +
                "g.check_object_id checkObjectId " +
                "from t_gradepoint g, t_bnp_supervision_task t where g.task_id = t.sys_id and t.aaa001 = '1' and t.task_result in ('1','3')");
        SQLQuery query = getSession().createSQLQuery(sql.toString());
        query.setResultTransformer(Transformers.aliasToBean(GradePoint.class));
        List<GradePoint> list = (List<GradePoint>) query.list();
        return list;
    }

    public void deletAllInvalidTaskGradePoint(List<String> taskIds){
        StringBuffer hql = (new StringBuffer("delete from GradePoint obj where obj.taskId in(:taskIds)"));
        Query query = this.getSession().createQuery(hql.toString());
        query.setParameterList("taskIds", taskIds);
        query.executeUpdate();
    }
}
